package org.jeecg.glr.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.glr.entity.GlrLedTask;
import org.jeecg.glr.service.IGlrLedTaskService;
import org.jeecg.glr.util.GLRConstant;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: LED屏定时任务
 * @Author: jeecg-boot
 * @Date:   2020-04-27
 * @Version: V1.0
 */
@RestController
@RequestMapping("/glr/glrLedTask")
@Slf4j
public class GlrLedTaskController extends JeecgController<GlrLedTask, IGlrLedTaskService> {
	@Autowired
	private IGlrLedTaskService glrLedTaskService;
	
	/**
	 * 分页列表查询
	 *
	 * @param glrLedTask
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(GlrLedTask glrLedTask,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<GlrLedTask> queryWrapper = QueryGenerator.initQueryWrapper(glrLedTask, req.getParameterMap());
		Page<GlrLedTask> page = new Page<GlrLedTask>(pageNo, pageSize);
		IPage<GlrLedTask> pageList = glrLedTaskService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param glrLedTask
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody GlrLedTask glrLedTask) {
		if(StringUtil.isNotEmpty(glrLedTask.getStatus())){
			glrLedTask.setStatus(GLRConstant.YN_N);
		}
		glrLedTaskService.save(glrLedTask);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param glrLedTask
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody GlrLedTask glrLedTask) {
		glrLedTaskService.updateById(glrLedTask);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		glrLedTaskService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.glrLedTaskService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		GlrLedTask glrLedTask = glrLedTaskService.getById(id);
		if(glrLedTask==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(glrLedTask);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param glrLedTask
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, GlrLedTask glrLedTask) {
        return super.exportXls(request, glrLedTask, GlrLedTask.class, "LED屏定时任务");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, GlrLedTask.class);
    }

	 /**
	  * 通过excel导入数据
	  *
	  * @return
	  */
	 @RequestMapping(value = "/stratTask", method = RequestMethod.POST)
	 public Result<?> stratTask(@RequestBody GlrLedTask glrLedTask) {
		Boolean flag= glrLedTaskService.stratTask(glrLedTask.getIds());
		if(flag){
			return Result.ok("操作成功");
		}
		 return Result.ok("操作失败");
	 }


}
